<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="eqName">
        <el-input
          v-model="queryParams.eqName"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所处位置" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入所处位置"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="施工单位" prop="constructionUnit">
        <el-input
          v-model="queryParams.constructionUnit"
          placeholder="请输入施工单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="责任人" prop="director">
        <el-input
          v-model="queryParams.director"
          placeholder="请输入责任人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备状态" prop="eqStatus">
            <el-select
              v-model="queryParams.eqStatus"
              placeholder="用户状态"
              clearable
              size="small"
              style="width: 240px"
            >
                <el-option
                  v-for="equipment in equipmentOptions"
                  :key="equipment.dictValue"
                  :label="equipment.dictLabel"
                  :value="equipment.dictValue"
                ></el-option>
            </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cscec:equipment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cscec:equipment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['cscec:equipment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cscec:equipment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备号" align="center" prop="eqId" />
      <el-table-column label="设备名称" align="center" prop="eqName" />
      <el-table-column label="所处位置" align="center" prop="location" />
      <el-table-column label="施工单位" align="center" prop="constructionUnit" />
      <el-table-column label="责任人" align="center" prop="director" />
      <el-table-column label="责任人电话" align="center" prop="directorPhone" />
      <el-table-column label="管理信息" align="center" prop="manaInfo" />
      <el-table-column label="设备连接参数信息" align="center" prop="connectParam" />
      <el-table-column label="设备状态" align="center" prop="eqStatus" >
            <template slot-scope="scope">
              <el-switch
                active-color="#13ce66"
                inactive-color="#ff4949"
                v-model="scope.row.eqStatus"
                :active-value="0"
                :inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cscec:equipment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="getInfo(scope.row)"
            v-hasPermi="['cscec:equipment:list']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cscec:equipment:remove']"
          >删除</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-map"
                      @click="handleMap"
                    >地图</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div class="mask"  v-show="mapVisible">
    <span class="closeBtn" @click="mapVisible=false">X</span>
    <Map v-show="mapVisible" class="mapId"/>
    </div>


    <el-dialog :title="title" :visible.sync="flowAndRepairVisible" width="800px" append-to-body>
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="联系人" name="f1">
          <el-collapse v-model="activeNames" @change="handleChange">
            <el-collapse-item title="流量" name="f2">
              <el-form ref="equipmentFlowAndRepair" :model="equipmentFlowAndRepair" :rules="rules" label-width="80px">
                <el-descriptions class="margin-top"  :column="2"  border>
                        <el-descriptions-item >
                          <template slot="label">
                                剩余流量
                          </template>
                          {{equipmentFlowAndRepair.flowRest}}
                        </el-descriptions-item>
                </el-descriptions>
              </el-form>
            </el-collapse-item>

            <el-collapse-item title="维修信息" name="f3">
             <el-form ref="repairList" :model="repairList" :rules="rules" label-width="100px">
                                 <el-table :data="repairList" @selection-change="handleSelectionChange" empty-text="暂无数据">
                                   <el-table-column label="设备号" align="center" prop="eqId"/>
                                   <el-table-column label="维修信息" align="center" prop="faultInfo" />
                                   <el-table-column label="维修时间" align="center" prop="repairTime" >
                                           <template slot-scope="scope">
                                             <span>{{ parseTime(scope.row.repairTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
                                           </template>
                                   </el-table-column>
                                 </el-table>
                               </el-form>
            </el-collapse-item>
            </el-collapse>
        </el-tab-pane>
    </el-tabs>
         <div slot="footer" class="dialog-footer">
           <el-button type="primary" @click="submitForm2">确 定</el-button>
           <el-button @click="cancel2">取 消</el-button>
         </div>
      </el-dialog>

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="eqName">
          <el-input v-model="form.eqName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="所处位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入所处位置" />
        </el-form-item>
        <el-form-item label="施工单位" prop="constructionUnit">
          <el-input v-model="form.constructionUnit" placeholder="请输入施工单位" />
        </el-form-item>
        <el-form-item label="责任人" prop="director">
          <el-input v-model="form.director" placeholder="请输入责任人" />
        </el-form-item>
        <el-form-item label="责任人电话" prop="directorPhone">
          <el-input v-model="form.directorPhone" placeholder="请输入责任人电话" />
        </el-form-item>
        <el-form-item label="管理信息" prop="manaInfo">
          <el-input v-model="form.manaInfo" placeholder="请输入管理信息" />
        </el-form-item>
        <el-form-item label="设备连接参数信息" prop="connectParam">
          <el-input v-model="form.connectParam" type="textarea" placeholder="请输入内容" />
        </el-form-item>
           <el-col :span="12">
             <el-form-item label="设备状态">
               <el-radio-group v-model="form.eqStatus">
                 <el-radio
                   v-for="dict in equipmentOptions"
                   :key="dict.dictValue"
                   :label="dict.dictValue"
                 >{{dict.dictLabel}}</el-radio>
               </el-radio-group>
             </el-form-item>
           </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Map from '@/components/map/index.vue'
import { listEquipment, getEquipment, delEquipment, addEquipment, updateEquipment,getFlowAndRepairInfo,changeUserStatus } from "@/api/cscec/equipment";

export default {
  name: "Equipment",
  components:{
    Map
  },
  data() {
    return {
      mapStatus: false,
      // 遮罩层
      mapVisible:false,
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      equipmentOptions:[],
      // 设备管理表格数据
      activeNames: ['1'],
      flowAndRepairVisible: false,
      activeName: 'second',
      equipmentList: [],
      equipmentFlowAndRepair: {},
      repairList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        eqName: null,
        location: null,
        constructionUnit: null,
        director: null,
        directorPhone: null,
        manaInfo: null,
        connectParam: null,
        eqStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },

  created() {
    this.getList();
        this.getDicts("sys_equipment").then(response => {
          this.equipmentOptions = response.data;
          console.log(response.data)
        });
  },

  methods: {
    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      listEquipment(this.queryParams).then(response => {
        this.equipmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleMap(){
      this.mapVisible=true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
                      handleChange(val) {
                                    },
               handleClick(tab, event) {
                    },
                                                  submitForm2() {
                                                    this.flowAndRepairVisible = false;
                                                    this.reset();
                                                  },
                        cancel2() {
                          this.flowAndRepairVisible = false;
                          this.reset();
                        },
    // 表单重置
    reset() {
      this.form = {
        eqId: null,
        eqName: null,
        location: null,
        constructionUnit: null,
        director: null,
        directorPhone: null,
        manaInfo: null,
        connectParam: null,
        eqStatus: 0,
        repairList: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.eqId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const eqId = row.eqId || this.ids
      getEquipment(eqId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.eqId != null) {
            updateEquipment(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEquipment(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    getInfo(row){
      this.flowAndRepairVisible= true;
      this.title='流量和维修情况';
      const eqId=row.eqId;
      getFlowAndRepairInfo(eqId).then(response => {
          this.equipmentFlowAndRepair=response.data
          this.repairList=response.data.equipmentRepairList;
      })
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.eqStatus === "0" ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.eqName + '"设备吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return changeUserStatus(row.eqId, row.eqStatus);
        }).then(() => {
          this.$msgSuccess(text + "成功");
        }).catch(function() {
          row.eqStatus = row.eqStatus === "0" ? "1" : "0";
        });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const eqIds = row.eqId || this.ids;
      this.$confirm('是否确认删除设备管理编号为"' + eqIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEquipment(eqIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cscec/equipment/export', {
        ...this.queryParams
      }, `cscec_equipment.xlsx`)
    }
  }
};
</script>
<style>
.el-dialog__wrapper{
  height:1000px;
}
.mask{
position:fixed;
width:100%;
height:100%;
background:rgba(0,0,0,0.6);
top:0;
left:0;
z-index:1;
}
.mapId{
position:fixed!important;
width:800px!important;
top:50%;
left:50%;
transform:translate(-50%,-50%);
z-index:2!important;
}
.closeBtn{
position:fixed;
top:120px;
right:300px;
width:25px;
height:25px;
background:rgba(255,255,255,0.7);
line-height:25px;
text-align:center;
color:#fff;
border-radius:50%;
cursor:pointer;
}

</style>
